package biz.innovasoft.nodb.matchers;

import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
import org.hamcrest.core.IsInstanceOf;

public class InstanceOf extends IsInstanceOf {

	private final boolean strict;
	private final Class<?> expectedClass;
	
	public InstanceOf(Class<?> expectedClass) {
		this(expectedClass, false);
	}
	
	public InstanceOf(Class<?> expectedClass, boolean strict) {
		super (expectedClass);
		this.expectedClass = expectedClass;
		this.strict = strict;
	}
	
	protected boolean matches(Object item, Description mismatchDescription) {
		if (null == item) {
			return false;
		}
		if (strict) {
			return item.getClass().equals(expectedClass);
		}
		return super.matches(item, mismatchDescription);
	}
	
	@SuppressWarnings("unchecked")
    @Factory
    public static <T> Matcher<T> instanceOf(Class<?> type, boolean strict) {
        return (Matcher<T>) new InstanceOf(type, strict);
    }

}
